<template>
  <div class="top-category">
    <div class="container m-top-20">
      <XtxSlider :sliders="sliders" />
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in categoryData" :key="item.id">
            <a href="javascript:;">
              <img :src="item.picture" />
              <p>{{ item.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in categoryData" :key="item.id">
        <div class="head">
          <h3>{{ item.name }}</h3>
          <XtxMore to="/" />
        </div>
        <div class="body">
          <RouterLink
            to="/"
            class="goods-item"
            v-for="good in item.goods"
            :key="good"
          >
            <img :src="good.picture" alt="" />
            <p class="name ellipsis">{{ good.name }}</p>
            <p class="desc ellipsis">{{ good.desc }}</p>
            <p class="price">&yen;{{ good.price }}</p>
          </RouterLink>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// import { ref } from 'vue'
import useBananer from '@/views/category/service/usebananer.js'
import useCategory from './service/useCategory'
// import { findBanner } from '@/api/home.js'
// import { findTopCategory } from '@/api/category.js'
// import { useRoute } from 'vue-router'
export default {
  name: 'TopCategory',
  setup () {
    // // 11.6定义轮播图数组
    // const sliders = ref([])
    // // 11.6调接口
    // async function loadSliders () {
    //   const res = await findBanner()
    //   sliders.value = res.result
    // }
    // 组合：setup只是作为一个用来组合其他逻辑的钩子，在内部事实上不写太多业务代码
    const { sliders } = useBananer()
    // 16.2获取分类接口
    // 16.2给一个空数组
    // const categoryData = ref([])
    // const route = useRoute()// 这个是可以在serup中拿取id，内置的
    // async function getCategroyAllList () {
    //   // 16.2因为需要传递参数，接口需要的
    //   const res = await findTopCategory(route.params.id)
    //   categoryData.value = res.result.children
    // }
    const { categoryData } = useCategory()
    // loadSliders()
    // getCategroyAllList()

    return {
      sliders,
      categoryData

    }
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }
}
</style>
<style lang="less" scoped>
.goods-item {
  display: block;
  width: 220px;
  padding: 20px 30px;
  text-align: center;
  img {
    width: 160px;
    height: 160px;
  }
  p {
    padding-top: 10px;
  }
  .name {
    font-size: 16px;
  }
  .desc {
    color: #999;
    height: 29px;
  }
  .price {
    color: @priceColor;
    font-size: 20px;
  }
}
</style>
